#!/usr/bin/perl
use strict;
use warnings;

#首先，文件通过参数传入
my $arg_len = @ARGV;
my @lines;
if($arg_len == 1) {
    my $p_file_path = $ARGV[0];
    open(PROC_FILE,$p_file_path) || die("open $p_file_path error $?");
    @lines = <PROC_FILE>;
} elsif($arg_len == 0) {
    @lines = <STDIN>;
}

my $dev_name;
my $inet_addr;
my %dev_ip_map;

for(@lines)
{
    chomp($_);
    #首先判断起始字符串
    if(m/^\w+/) {
        #表明是设备文件
        $dev_name = $_;
        $dev_name=~s/\s+.*//i;
    } else {
        #取出IP地址
        if(m/inet /) {
            $inet_addr = $_;
            $inet_addr=~s/^\s+//i;
            $inet_addr=~s/inet /ipv4/i;
            my @kv_str_arr = split(' ',$inet_addr);
            for(@kv_str_arr) {
                if(m/ipv4/) {
                    my @kv_arr = split(':',$_);
                    #将dev name和ipv4地址绑定在一起
                    $dev_ip_map{$dev_name} = $kv_arr[1];
                }
            }
        }
    }
}

while((my $k,my $v) = each %dev_ip_map) {
    print "$k $v\n";
}

if($arg_len == 1) {
    close(PROC_FILE);
}

